import { defineConfig } from "vite";
import uni from "@dcloudio/vite-plugin-uni";
import { UnifiedViteWeappTailwindcssPlugin as uvwt } from "weapp-tailwindcss/vite";
import * as fs from "fs";

const content = fs
  .readFileSync(`${__dirname}/.env.${process.env.ENV ?? "prod"}`, "utf-8")
  .split("\n")
  .filter((line) => line.trim() !== "" && !line.trim().startsWith("#"))
  .reduce((acc, line) => {
    const [key, value] = line.split("=");
    acc[key.trim()] = value.trim().replace(/^['"]|['"]$/g, ""); // 去掉多余的引号
    return acc;
  }, {});

console.log("环境变量", content);

export default defineConfig({
  // uni 是 uni-app 官方插件， uvtw 一定要放在 uni 后，对生成文件进行处理
  plugins: [uni(), uvwt()],
  define: Object.entries(content).reduce((acc, [key, value]) => {
    acc[`process.env.${key}`] = JSON.stringify(value);
    return acc;
  }, {}),
  css: {
    postcss: {
      plugins: [
        // require('tailwindcss')() 和 require('tailwindcss') 等价的，表示什么参数都不传，如果你想传入参数
        // require('tailwindcss')({} <- 这个是postcss插件参数)
        require("tailwindcss"),
        require("autoprefixer"),
      ],
    },
  },
});
